/**定义证件照使用的数据配置类,包括页面配置数据以及从后台获取的证件照参数 */
import {
    Config
} from '../utils/config';
import {
    API
} from "../utils/fireapi";


// 首页功能按钮
const shop = [
    {
        title: '证件照',
        type: 'detail',
        sub: [{
            navPath: "/pages/photo/camera/camera",
            title: "制作证件照",
            desc: "拍摄或从相册选择一张照片制作证件照",
            iconPath: "/images/icons/home-make-photo.png",
            bgColor: 'blue',
            // openType:"switchTab"

        },
        {
            navPath: "/pages/photo/background/background",
            title: "更换底色",
            desc: "将标准证件照底色修改为所需颜色",
            iconPath: "/images/icons/home-change-bg.png",
            bgColor: 'orange',
        },
        {
            navPath: "/pages/photo/cropper/cropper",
            title: "旋转与裁剪",
            desc: "将图片裁剪为标准照片或自定义尺寸",
            iconPath: "/images/icons/home-cut.png",
            bgColor: 'olive',
        },
        {
            navPath: "/pages/photo/compress/compress",
            title: "压缩图片",
            desc: "将图片文件压缩为指定大小",
            iconPath: "/images/icons/home-compress.png",
            bgColor: 'cyan',
        },
        ]
    },
    {
        title: '特效',
        type: 'simple',
        sub: [{
            navPath: "/pages/photo/anime/anime",
            title: "风格化",
            iconPath: "/images/icons/pen.png",

        }
        ]
    }
]


/**重新组织广告信息内容*/
function assemblyAd(ads) {
    // 初始化广告对象结构
    let admitisement = {
        [Config.AD_POSITION_WELCOME]: { enable: false, customImg: [] },
        [Config.AD_POSITION_HOME]: { enable: false, customImg: [] },
        [Config.AD_POSITION_CASHIER]: { enable: false, customImg: [] },
        [Config.AD_POSITION_PAYMENT]: { enable: false, customImg: [] },
        [Config.AD_POSITION_WAIT]: { enable: false, customImg: [] },
    }
    if(ads){
        ads.forEach(item => {
            admitisement[item.position]["enable"] = true;
            admitisement[item.position]["source"] = item.source;
            admitisement[item.position]["unitId"] = item.adid;
            admitisement[item.position]["customImg"].push({
                title: item.title,
                img: item.img
            });
        })

    }

    return admitisement;
}

/**小程序的名称图片等设置，从后台获取 */
function miniConfig(config) {
    let ret = {}
    if (config.length > 0) {
        ret = {
            mpName: config[0].app_name,
            shareTitle: config[0].share_title,
            sharePath: config[0].share_path,
            shareImage: config[0].share_img,
            waitPageImg: config[0].wait_logo || "/images/icons/loading.gif",
            waitPageTxt: config[0].wait_text || "正在处理,请稍后...",
        }
    }
    return ret;
}

/**功能价格处理 */
function funcPrice(func) {
    // 初始值功能价格对象结构
    let ret = {
        [Config.IDENTITY_MAKEPHOTO]: {
            id: Config.IDENTITY_MAKEPHOTO,
            price: 0
        },
        [Config.IDENTITY_CROP]: {
            id: Config.IDENTITY_CROP,
            price: 0
        },
        [Config.IDENTITY_CHANGEBG]: {
            id: Config.IDENTITY_CHANGEBG,
            price: 0
        },
        [Config.IDENTITY_COMPRESS]: {
            id: Config.IDENTITY_COMPRESS,
            price: 0
        },
        [Config.IDENTITY_ANIME]: {
            id: Config.IDENTITY_ANIME,
            price: 0
        },

    }
    // 根据后端结果设置功能价格
    if (func.length > 0) {
        ret[Config.IDENTITY_MAKEPHOTO].price = func[0].make_idphoto;
        ret[Config.IDENTITY_CROP].price = func[0].clip_photo;
        ret[Config.IDENTITY_CHANGEBG].price = func[0].change_bg;
        ret[Config.IDENTITY_COMPRESS].price = func[0].compress_photo;
        ret[Config.IDENTITY_ANIME].price = func[0].human_style;
    }
    return ret;
}
//加载数据
export default function loadServerData() {
    return new Promise((resolve, reject) => {
        Promise.all([
            API.getAllSizeTypes(),
            API.getAllBgs(),
            API.getAd(),
            API.getMiniConfig(),
            API.getProductPrice(),
        ]).then((values) => {
            const data = {
                shop: shop,
                categorys: values[0],
                bgs: values[1],
                ad: assemblyAd(values[2]),
                config: miniConfig(values[3]),
                funcPrice: funcPrice(values[4]),
            }
            wx.setStorageSync('ServerData', data)
            resolve(data)
        });
    })

}